﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TestSimulator.ServiceReference1;

namespace TestSimulator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private PumpServiceClient _pumpService;
        private Dictionary<int, WaterThread> _waterThreads;
        private Dictionary<int, WaterMonitor> _monitors;
        private Dictionary<int, Thread> _rainThreads;
        private Dictionary<int, Thread> _drainThreads;

        public MainWindow()
        {
            InitializeComponent();
            _pumpService = new PumpServiceClient();
            SimulateThreads(_pumpService);
        }

        private void SimulateThreads(PumpServiceClient service)
        {
            _waterThreads = new Dictionary<int, WaterThread>();
            _monitors = new Dictionary<int, WaterMonitor>();
            _rainThreads = new Dictionary<int, Thread>();
            _drainThreads = new Dictionary<int, Thread>();

            foreach (Station s in _pumpService.RequestCurrentStatusForAll())
            {
                Dictionary<int, double> pumps = _pumpService.RequestCurrentStatus(s.Id).ToDictionary(p => p.Id, p => p.Capacity);
                WaterMonitor monitor = new WaterMonitor();
                WaterThread thread = new WaterThread(monitor, s.Id, pumps, 0, 1000);
                Thread rain = new Thread(thread.Rain);
                _rainThreads.Add(s.Id, rain);

                foreach (Pump p in _pumpService.RequestCurrentStatus(s.Id))
                {
                    Thread drain = new Thread(thread.Drain);
                    _drainThreads.Add(p.Id, drain);
                }

                monitor.LevelValue += LevelHandle;
                monitor.RainValue += RainHandle;
                monitor.DrainValue += DrainHandle;

                _monitors.Add(s.Id, monitor);
                _waterThreads.Add(s.Id, thread);
            }

            foreach (int id in _rainThreads.Keys)
            {
                _rainThreads[id].Start();
            }

            foreach (int id in _drainThreads.Keys)
            {
                _drainThreads[id].Start(id);
            }
        }

        //start pump, edit in status in db and thread
        private void StartPump()
        {

        }

        //stop pump, edit in status in db and thread
        private void StopPump()
        {

        }
        private void LevelHandle(double value)
        {
            //MessageBox.Show(" tst : " + value);
        }
        private void RainHandle(double value)
        {
            //MessageBox.Show(" tst : " + value);
        }
        private void DrainHandle(double value)
        {
            //MessageBox.Show(" tst : " + value);
        }
    }
}
